<?php
namespace app\mingteng\controller;

use think\Cache;
use think\Page;
use app\mingteng\model\Navigation;

class System extends Base{
	/*
	 * 配置入口
	 */
	public function index()
	{
		/*配置列表*/
		$group_list = [
            'index' => '网站信息',
            'sms'       => '短信设置',
//            'file'     => '文件材料模板',
//            'push'      => '推送设置',
        ];		
		$this->assign('group_list',$group_list);
		$inc_type =  I('get.inc_type','index');
		$this->assign('inc_type',$inc_type);
		$config = tpCache($inc_type);
		$this->assign('config',$config);//当前配置项
		return $this->fetch($inc_type);
	}
	
	/*
	 * 新增修改配置
	 */
	public function handle(){
		$param = I('post.');
		$inc_type = $param['inc_type'];
		unset($param['inc_type']);
		tpCache($inc_type,$param);
		$this->success("操作成功",U('System/index',array('inc_type'=>$inc_type)));
	}        
        
       /**
        * 栏目列表
        */
    public function navigationList(){
        $m_nav = new Navigation();
        $navigationList = $m_nav->all_list();

       $this->assign('navigationList',$navigationList);
       return $this->fetch('navigationList');
     }

    /**
     * 添加修改编辑 栏目
     */
    public function addEditNav(){
        $model = D("Navigation");
        if (IS_POST) {
            if (I('id')){
                if(I('pid')>0){
                    $p = $model->where(array('id'=>I('pid')))->find();
                    if($p['pid'] == I('id')){
                        $this->error("不能选取自己的子分类为父级");
                    }
                }
                $model->update(I('post.'));
            }else{
                $model->add(I('post.'));
            }
            $this->success("操作成功!", U('System/navigationList'));
        }
        // 点击过来编辑时
        $id = I('id',0);
        $m_nav = new Navigation();
        $navigation = $m_nav->where('id',$id)->find();
        $system_nav = $m_nav->sort_list();
        $this->assign('system_nav', $system_nav);
        $this->assign('navigation', $navigation);
        return $this->fetch('_navigation');
    }
    
    /**
     * 删除栏目
     */
    public function delNav(){
        $nav = M('Navigation');
        if($nav->where("pid",I('id'))->find()){
            $this->error("请先删除子分类！");
        }
        $nav->where("id",I('id'))->delete();
        $this->success("操作成功!!!",U('System/navigationList'));
    }

    /**
     * 清空系统缓存
     */
    public function cleanCache(){
        delFile(RUNTIME_PATH);
        Cache::clear();
        $this->success("操作完成!",U('Index/welcome'));
    }

     //友情链接列表
    public function linkList(){
        $Ad =  M('friend_link');
        $p = $this->request->param('p');
        $list = array();
        $k_w = trim(I('keywords'));
        $where = '';
        if(!empty($k_w)){
            $where = ['link_name'=>['like','%'.$k_w.'%']];
        }
        $res = $Ad->where($where)->order('orderby')->page($p.',10')->select();
        if($res){
            foreach ($res as $val){
                $val['target'] = $val['target']>0 ? '开启' : '关闭';
                $list[] = $val;
            }
        }
        $this->assign('list',$list);// 赋值数据集
        $count = $Ad->where($where)->count('link_id');// 查询满足要求的总记录数
        $Page = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->assign('pager',$Page);// 赋值分页输出
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch('linkList');
    }
    //填写友情链接
    public function link(){
        $act = I('get.act','add');
        $this->assign('act',$act);
        $link_id = I('get.link_id/d');
        $link_info = array();
        if($link_id){
            $link_info = D('friend_link')->where('link_id', $link_id)->find();
        }
        $this->assign('info',$link_info);
        return $this->fetch();
    }
    //提交友情链接
    public function linkHandle(){
        $data = I('post.');
        if($data['act'] == 'add'){
            stream_context_set_default(array('http'=>array('timeout' =>2)));
            $r = D('friend_link')->insert($data);
        }
        if($data['act'] == 'edit'){
            $r = D('friend_link')->where('link_id', $data['link_id'])->save($data);
        }

        if($data['act'] == 'del'){
            $r = D('friend_link')->where('link_id', $data['del_id'])->delete();
            if($r) exit(json_encode(1));
        }
        if($r){
            $this->success("操作成功",U('System/linkList'));
        }else{
            $this->error("操作失败",U('System/linkList'));
        }
    }
    /**
     *  管理员登录后 处理相关操作
     */
    public function login_task(){
        $time = time() - 2592000;
        $f_time = time() - 432000;

        // 删除 cart表垃圾数据 删除一个月以前的
        M("cart")->where("add_time < $time")->delete();

        // 删除sms_log表垃圾数据 删除一个月以前的短信
        M("sms_log")->where("add_time < $time")->delete();

//        M("ex_order")->where("add_time < $f_time AND status = 0")->delete();
//        $order = M("order")->where("add_time < $f_time AND order_status = 0")->select();
//        foreach($order as $k=>$v){
//            M("order_goods")->where(['order_id'=>$v['order_id']])->delete();
//        }
//        M("order")->where("add_time < $f_time AND order_status = 0")->delete();
    }
}